package com.yyy.dm.consumer.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.yyy.dm.CommonResponse;
import com.yyy.dm.VoUtil;
import com.yyy.dm.consumer.feign.base.DMImageFeign;
import com.yyy.dm.dao.ItemTypeMapper;
import com.yyy.dm.item.*;
import org.springframework.beans.BeanUtils;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.io.IOException;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

@Service
public class ItemTypeConsumerService {

    @Resource
    private ItemTypeMapper itemTypeMapper;
    @Resource
    private DMImageFeign dmImageFeign;
    @Resource
    private RedisTemplate<String,Object> redisTemplate;

    public CommonResponse nav() throws IOException {
        ObjectMapper mapper = new ObjectMapper();
        Object value = redisTemplate.opsForValue().get("index:nav");
        String str = value ==null ?null:value.toString();
        List<IndexNavTypeDTO> result = null;
        if(str==null){
            List<IndexNavTypeDTO> itemTypeList = itemTypeMapper.findItemType();
            for (IndexNavTypeDTO indexNavTypeDTO : itemTypeList) {
                List<IndexNavHotItemsDTO> indexNavHotItemsDTOList = indexNavTypeDTO.getHotItems();
                if (indexNavHotItemsDTOList != null && indexNavHotItemsDTOList.size() > 0) {
                    for (IndexNavHotItemsDTO indexNavHotItemsDTO : indexNavHotItemsDTOList) {
                        indexNavHotItemsDTO.setImgUrl(dmImageFeign.findItemPosterImageByItemId(indexNavHotItemsDTO.getId()));
                    }
                }
                redisTemplate.opsForValue().set("index:nav",mapper.writeValueAsString(itemTypeList));
                result = itemTypeList;
            }
        }else {
            result = mapper.readValue( str,mapper.getTypeFactory().constructParametricType(List.class,IndexNavTypeDTO.class));
        }
        return VoUtil.returnSuccess(result);
    }

    public CommonResponse lineNav(){
        List<IndexLineNavTypeDTO> lineNavTypeDTOList = itemTypeMapper.findIndexLineItemType();
        return VoUtil.returnSuccess(lineNavTypeDTOList);
    }

    public CommonResponse floor(){
        List<IndexFloorDTO> indexFloorList = itemTypeMapper.findIndexFloor();
        for (IndexFloorDTO indexFloorDTO : indexFloorList) {
            List<ItemFloorDTO> items = indexFloorDTO.getItems();
            for (int i = 0; i < items.size(); i++) {
                ItemFloorDTO itemFloorDTO = items.get(i);
                 if(i==0){
                     itemFloorDTO.setImgUrl(dmImageFeign.findItemPosterImageByItemId(itemFloorDTO.getId()));
                 }else {
                     itemFloorDTO.setImgUrl(dmImageFeign.findItemCardImageByItemId(itemFloorDTO.getId()));
                 }
            }
        }
        return VoUtil.returnSuccess(indexFloorList);
    }

    public CommonResponse getRecommend(Long itemTypeId){
        List<ItemFloorDTO> itemFloorDTOList = itemTypeMapper.findItemFloorByItemTypeId(itemTypeId);
        for (ItemFloorDTO itemFloorDTO : itemFloorDTOList) {
            itemFloorDTO.setImgUrl(dmImageFeign.findItemPosterImageByItemId(itemFloorDTO.getId()));
        }
        return VoUtil.returnSuccess(itemFloorDTOList);
    }

    public CommonResponse sortGoods(Long parent){
        QueryWrapper<DmItemType> queryWrapper = new QueryWrapper();
        queryWrapper.eq("parent",parent);
        List<DmItemType> dmItemTypeList = itemTypeMapper.selectList(queryWrapper);
        List<ListItemTypeDTO> listItemTypeDTOList = new ArrayList<>();
        for (DmItemType dmItemType : dmItemTypeList) {
            ListItemTypeDTO listItemTypeDTO = new ListItemTypeDTO();
            BeanUtils.copyProperties(dmItemType,listItemTypeDTO);
            listItemTypeDTOList.add(listItemTypeDTO);
        }
        return VoUtil.returnSuccess(listItemTypeDTOList);
    }

    public CommonResponse getCarouselData(Long itemTypeId){
        List<MultipleItemBannerDTO> multipleItemBannerDTOList = itemTypeMapper.findMultipleItemBannerByItemTypeId(itemTypeId);
        for (MultipleItemBannerDTO multipleItemBannerDTO : multipleItemBannerDTOList) {
            multipleItemBannerDTO.setImgUrl(dmImageFeign.findItemBannerByItemId(multipleItemBannerDTO.getId()));
        }
        return VoUtil.returnSuccess(multipleItemBannerDTOList);
    }

    public CommonResponse getGuessYouLike(Long itemTypeId){
        List<ItemFloorDTO> itemFloorDTOList = itemTypeMapper.findItemFloorByItemTypeIdGuessLike(itemTypeId);
        for (ItemFloorDTO itemFloorDTO : itemFloorDTOList) {
            itemFloorDTO.setImgUrl(dmImageFeign.findItemPosterImageByItemId(itemFloorDTO.getId()));
        }
        return VoUtil.returnSuccess(itemFloorDTOList);
    }

    public CommonResponse getWonderfulData(Long itemTypeId){
        List<ItemFloorDTO> itemFloorDTOList = itemTypeMapper.findItemFloorByItemTypeIdWonderfulData(itemTypeId);
        for (ItemFloorDTO itemFloorDTO : itemFloorDTOList) {
            itemFloorDTO.setImgUrl(dmImageFeign.findItemPosterImageByItemId(itemFloorDTO.getId()));
        }
        return VoUtil.returnSuccess(itemFloorDTOList);
    }

    public CommonResponse getParentChild(Long ageGroup,Long limit){
        List<ItemFloorDTO> itemFloorDTOList = itemTypeMapper.findItemFloorByAgeGroup(new HashMap() {{
            put("ageGroup", ageGroup);
            put("limit", limit);
        }});
        for (ItemFloorDTO itemFloorDTO : itemFloorDTOList) {
            itemFloorDTO.setImgUrl(dmImageFeign.findItemChildByItemId(itemFloorDTO.getId()));
        }
        return VoUtil.returnSuccess(itemFloorDTOList);
    }

    public CommonResponse getPopularSearch(Long itemTypeId){
        List<ItemFloorDTO> itemFloorDTOList = itemTypeMapper.findItemFloorByItemTypeIdPopularSearch(itemTypeId);
        for (ItemFloorDTO itemFloorDTO : itemFloorDTOList) {
            itemFloorDTO.setImgUrl(dmImageFeign.findItemCardImageByItemId(itemFloorDTO.getId()));
        }
        return VoUtil.returnSuccess(itemFloorDTOList);
    }

    public CommonResponse getAdimgUrl(Long itemTypeId){
        List<ItemFloorDTO> itemFloorDTOList = itemTypeMapper.findItemFloorByItemTypeIdAdImage(itemTypeId);
        for (ItemFloorDTO itemFloorDTO : itemFloorDTOList) {
            itemFloorDTO.setImgUrl(dmImageFeign.findItemTransparentByItemId(itemFloorDTO.getId()));
        }
        return VoUtil.returnSuccess(itemFloorDTOList);
    }

    public CommonResponse getCalendarData(Long itemTypeId){
        Integer year = LocalDateTime.now().getYear();
        Integer month = LocalDateTime.now().getMonthValue();
        HashMap hashMap = new HashMap() {{
            put("year",year);
            put("itemTypeId", itemTypeId);
            put("month", month);
        }};
        List<String> days = itemTypeMapper.findDaysByMonthAndItemTypeId(hashMap);
        List<CalendarDataDTO> calendarDataDTOList = new ArrayList<>();
        for (String day : days) {
            CalendarDataDTO calendarDataDTO = new CalendarDataDTO();
            calendarDataDTO.setDay(day.replace("-",""));
            hashMap.put("day",day);
            List<ItemFloorDTO> itemFloorDTOList = itemTypeMapper.findItemFloorByMonthAndDay(hashMap);
            for (ItemFloorDTO itemFloorDTO : itemFloorDTOList) {
                itemFloorDTO.setImgUrl(dmImageFeign.findItemCardImageByItemId(itemFloorDTO.getId()));
            }
            calendarDataDTO.setItemList(itemFloorDTOList);
            calendarDataDTOList.add(calendarDataDTO);
        }

        return VoUtil.returnSuccess(calendarDataDTOList);
    }

}